/**
 * 19/02/2014
 * 
 * SupplierController
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.SupplierBOInterface;
import org.cuatrovientos.erp.model.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Controller class for Supplier management
 * @author Javier Tejedor
 */
@Controller
public class SupplierController {

	//Get log4j logger
	private static final Logger logger = 
			Logger.getLogger(SupplierController.class);

	@Autowired
	private SupplierBOInterface supplierBO;

	/**
	 * default constructor
	 */
	public SupplierController () {
		logger.info("Supplier BEAN instantiated");
	}

	/**
	 * handles default /supplier or /supplier/list request
	 * @return
	 */
	@RequestMapping({"/supplier","/supplier/list"})
	public ModelAndView supplierDefault() {
		logger.info("default");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Supplier> supplierList = supplierBO.selectAll();
		model.put("suppliers", supplierList );
		
		return new ModelAndView("SupplierList", model);     
	}


	/**
	 * handles default /supplier/search request
	 * @param name
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/supplier/search"})
	public ModelAndView search(
			@RequestParam(value="name", defaultValue="", required=true)
			String name) {
		
		logger.info("search");

		Map<String, Object> model = new HashMap<String, Object>();

		List<Supplier> supplierList = supplierBO.search(name);
		model.put("suppliers", supplierList);
		
		return new ModelAndView("SupplierList", model);     
	}

	/**
	 * handles default /supplier/delete request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/supplier/delete"})
	public String deleteSupplier(
			@RequestParam(value="id", defaultValue="", required=true) int id) {
		
		logger.info("delete " + id);

		supplierBO.deleteById(id);
		
		return "redirect:/supplier";
	}

	/**
	 * handles default /supplier/new request
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/supplier/new"})
	public ModelAndView newSupplier() {
		logger.info("new supplier");
		
		Supplier supplier = new Supplier();

		return new ModelAndView("NewSupplier", "supplier", supplier);
	}

	/**
	 * handles default /supplier/save request
	 * @param supplier
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/supplier/save"})
	public String newSupplier(@Valid Supplier supplier, BindingResult bindingResult) {
		logger.info("new supplier " + supplier.toString());

		//In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("supplier", supplier);
			
			return "NewSupplier";
		}

		supplierBO.insert(supplier);
		
		return "redirect:/supplier";
	}

	/**
	 * handles default /supplier/detail request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/supplier/detail"})
	public ModelAndView supplierDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("Detail " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Supplier supplier = supplierBO.selectById(id);
		model.put("supplier", supplier);

		return new ModelAndView("SupplierDetail", model);
	}


	/**
	 * handles default /supplier/update request
	 * @param id
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET,value={"/supplier/update"})
	public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
		logger.info("update " + id);
		
		Map<String,Object> model = new HashMap<String,Object>();
		Supplier supplier = supplierBO.selectById(id);
		model.put("supplier", supplier);
		
		return new ModelAndView("UpdateSupplier",model);
	}


	/**
	 * handles default /supplier/saveupdate request
	 * @param supplier
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(method=RequestMethod.POST,value={"/supplier/saveupdate"})
	public String saveUpdate(@Valid Supplier supplier, BindingResult bindingResult) {
		logger.info("update supplier " + supplier.toString());

		// In case of validation errors, go back to form
		if (bindingResult.hasErrors()) {
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("supplier", supplier);
			
			return "UpdateSupplier";
		}

		supplierBO.update(supplier);
		
		return "redirect:/supplier";
	}
	
	/**
	 * @param model
	 * @return
	 */
	@RequestMapping({"/supplier/index"})
	public String sayHelloPage(Map<String, Object> model) {
		return "index/IndexSupplier";
	}
	
}
